package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-29 13:29
 **/

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {
    /*
     * 发布短视频:
     *   路径：POST /smallVideos
     *   参数：videoThumbnail
     *           videoFile  返回值：null
     *   业务逻辑:
     *       1.文件存储到服务上
     *       2.保存到数据库表中
     */

    @Autowired
    private SmallVideosService videosService;

    @PostMapping
    public ResponseEntity smallVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideo(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }


    /*
     * 查询视频列表
     *   GET  /smallVideos
     *   请求参数： page,pagesize
           返回值：PageResult List<VideoVo>
     */



    @GetMapping
    public ResponseEntity findVideos(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult vo = videosService.findVideos(page, pagesize);
        return ResponseEntity.ok(vo);
    }

    /*
    * 视频点赞：
    *       POST  /:id/like
    * 参数：
    *       视频id
    * 返回数据: 空
    * */
    @PostMapping("/{id}/like")
    public  ResponseEntity like(@PathVariable("id" ) String videoId) {
        videosService.like(videoId);
        return ResponseEntity.ok(null);
    }
    /**
     * 小视频取消点赞
     * POST/smallVideos/:id/dislike
     * <p>
     * integer 点赞数
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity disLikeVideo(@PathVariable("id") String videoId) {

        Integer num = videosService.disLikeVideo(videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 小视频评论
     * GET /smallVideos/:id/comments
     * 路径参数   id 视频id
     * 评论数
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity commentVideo(@PathVariable("id") String videoId, @RequestBody Map map) {


        Integer num = videosService.videoSaveComments(videoId, map.get("comment").toString());
        return ResponseEntity.ok(null);
    }

    /**
     * 小视频评论列表
     * GET /smallVideos/:id/comments
     * 路径参数   id 评论id
     * Query：
     * 参数名称	备注
     * page		当前页数
     * pagesize	页尺寸
     * 返回数据
     * PageResult<VideoVo>
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                   @PathVariable("id") String videoId) {
        System.out.println("从网页获取到的评论参数" + page + pageSize + "!!!!!" + videoId);
        PageResult pageResult = videosService.findVideoComments(page, pageSize, videoId);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 小视频关注
     * POST   /smallVideos/:uid/userFocus
     * 路径参数   id 被关注人id
     * 无返回
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity focus(@PathVariable("uid") String toUserId) {

        videosService.focus(toUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 小视频评论列表
     * POST   /smallVideos/:uid/userUnFocus
     * 路径参数   id 被关注人id
     * 无返回
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity unFocus(@PathVariable("uid")String toUserId){

        videosService.unFocus(toUserId);
        return ResponseEntity.ok(null);

    }

}
